export function downloadFile(data, disposition, isDisposition = true) {
  /**
   *  接受二进制文件，下载文件
   *  */
  let filename;
  if (disposition) {
    if (disposition) {
      const dispositionAy = disposition.split(';');
      filename = dispositionAy.find((name) => /^filename=/.test(name));
      filename = filename.replace('filename=', '');
    } else {
      filename = disposition;
    }
  } else {
    filename = `文件${new Date().getTime()}`;
  }
  const url = window.URL.createObjectURL(new Blob([data]));
  const link = document.createElement('a');
  link.style.display = 'none';
  link.href = url;
  link.setAttribute('download', decodeURIComponent(filename));
  document.body.appendChild(link);
  link.click();
  // 释放URL 对象
  window.URL.revokeObjectURL(link.href);
  document.body.removeChild(link);
}

export const formatNumber = function (number, digit = 2) {
  /**
   *  把一个数字四舍五入为一个指定精度的数字
   *  */
  if (number === null || number === undefined) {
    return '';
  }
  const m = Math.pow(10, digit);
  const resetNum = `${Math.round(number * m) / m}`;
  const newNum = `${resetNum}.${new Array(digit).fill(0).join('')}`;
  return newNum.replace(/([\s\S]*)(\.)([\s\S]*)(\.)([\s\S]*)/g, (match, $1, $2, $3) => {
    const $3Temp = $3;
    if ($3Temp.length < digit) {
      $3Temp.padEnd('0', 2);
    }
    return $1 + $2 + $3Temp;
  });
};
